package com.arthas.arthasforsba.service;


import com.arthas.arthasforsba.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * 用户验证处理
 *
 * @author dl
 */
@Service
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Value("${spring.security.user.name}")
    private String adminName;

    @Value("${spring.security.user.password}")
    private String adminPassword;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username) ||
                !StringUtils.equals(username,adminName)) {
            log.info("登录用户：{} 不存在.", username);
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
        }

        return new LoginUser(adminName,adminPassword);
    }

//    public UserDetails createLoginUser(SysUser user) {
//        Set<String> postCode = sysPostService.selectPostCodeByUserId(user.getUserId());
//        postCode = postCode.parallelStream().map( s ->  "GROUP_" + s).collect(Collectors.toSet());
//        postCode.add("ROLE_ACTIVITI_USER");
//        List<SimpleGrantedAuthority> collect = postCode.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList());
//        return new LoginUser(user, permissionService.getMenuPermission(user), collect);
//    }
}
